package com.xfyuao.web;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xfyuao.dto.StuDto;
import com.xfyuao.entity.Stu;
import com.xfyuao.service.StuService;
import com.xfyuao.util.Result;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xfyuao
 * @since 2020-06-11
 */
@RestController
@RequestMapping("/stu")
public class StuController {

	@Autowired
	private StuService stuService;

	/**
	 * http://localhost:8080/insert 新增，id自增
	 * 
	 * @param stu
	 * @return 0代表成功
	 */
	@RequestMapping("/insert")
	public Object insert(Stu stu) {
		System.out.println("接收到的要新增的stu:" + stu);
		boolean bl = stuService.save(stu);
		if (bl) {
			return new Result(0, "添加成功！");
		}
		return new Result(2, "添加失败！");
	}

	/**
	 * http://localhost:8080/delete 根据id删除
	 * 
	 * @param stuNo
	 * @return 0代表成功
	 */
	@RequestMapping("/delete")
	public Object delete(Integer stuId) {
		boolean bl = stuService.removeById(stuId);
		if (bl) {
			return new Result(0, "删除成功！");
		}
		return new Result(1, "删除失败！");
	}

	/**
	 * http://localhost:8080/update 修改
	 * 
	 * @param stu
	 * @return 0代表成功
	 */
	@RequestMapping("/update")
	public Object update(Stu stu) {
		boolean bl = stuService.updateById(stu);
		if (bl) {
			return new Result(0, "修改成功！");
		}
		return new Result(1, "修改失败！");
	}

	/**
	 * http://localhost:8080/selectPage 动态条件分页查
	 * 使用了mybatis plus的Page<T>对象接收分页条件，
	 * 前台页面laytable渲染时，请求参数修改为current和size
	 * @param stuDto 查询条件
	 * @return layui数据表格要的数据格式
	 */
	@RequestMapping("/selectPage")
	public Object selectPage(StuDto st, Page<Stu> page) {
		System.out.println("stu查询条件：" + st);
		
		// 出入条件和分页数据，调用分页方法查询
		//Page<Stu> p = stuService.page(page, myWhere(stuDto));
		IPage<Stu> p = stuService.selectStuPage(page, st);
		
		return new Result(0, "查询成功", p.getRecords(), p.getTotal());
	}
	
}
